<?php


namespace App\Http\Model\Job;


use App\Enum\ClassAmtTransferEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadQueryTransferBillModel
{


    /**
     * 划拨/解冻结果查询   机构端 -> 招行
     *
     * @param $data
     * @param $result
     */
    public static function ins($data, $result)
    {
        $message = empty($result->message) ? '' : $result->message;
        /*未划拨处理*/
        if (empty($result->code) || $result->code !== 200) {
            /*课程划拨金额申请单据总表*/
            DB::table('class_amt_transfer')
              ->where('pfs_dw_id', $data['pfs_dw_id'])
              ->where('transfer_id', $data['transfer_id'])
              ->update(['pull_time' => date('Y-m-d H:i:s'), 'pfs_desc' => $message]);
            /*课程划拨金额申请单据明细表*/
            DB::table('class_amt_transfer_detail')->where('pfs_dw_id', $data['pfs_dw_id'])
              ->where('transfer_id', $data['transfer_id'])
              ->update(['pull_time' => date('Y-m-d H:i:s'), 'pfs_desc' => $message]);
            return 0;
        }
        if (empty($result->data) || empty($result->data->transferList)) {
            throw new \Exception('划拨&解冻结果查询,招行返回数据异常');
        }
        /*划拨成功处理*/
        DB::table('class_amt_transfer')
          ->where('pfs_dw_id', $data['pfs_dw_id'])
          ->where('transfer_id', $data['transfer_id'])
          ->update([
              'pull_time'              => date('Y-m-d H:i:s'),
              'pfs_desc'               => $message,
              /*招行返回-划拨课程总数*/
              'pfs_transfer_class_num' => $result->data->transferClassNum,
              /*招行返回-划拨总金额*/
              'pfs_sum_transfer_amt'   => $result->data->sumTransferAmt,
              /*招行返回-划拨完成日期，格式：YYYY-MM-DD，成功时必传*/
              'pfs_transfer_date'      => empty($result->data->transferDate) ? '' : $result->data->transferDate,
              /*招行返回-银行划拨流水，划拨成功时必传*/
              'pfs_tran_serial'        => empty($result->data->tranSerial) ? '' : $result->data->tranSerial,
          ]);
        /*课程划拨金额申请单据明细表*/
        foreach ($result->data->transferList as $val) {
            DB::table('class_amt_transfer_detail')
              ->where('pfs_dw_id', $data['pfs_dw_id'])
              ->where('transfer_id', $data['transfer_id'])
              ->where('sub_transfer_id', $val->subTransferId)
              ->update([
                  'pull_time'           => date('Y-m-d H:i:s'),
                  'pfs_desc'            => empty($val->desc) ? $message : $val->desc,
                  /*S:校验成功、 F：校验失败*/
                  'pfs_transfer_status' => $val->transferStatus,
                  /*资金类型：00 正常拨付、01 退款拨付*/
                  'pfs_amount_type'     => $val->amountType,
                  /*此班级剩余金额*/
                  'pfs_surplus_amt'     => $val->surplusAmt,
                  /*此班级累计已经划拨金额*/
                  'pfs_transferred_amt' => $val->transferAmt,
                  /*本次划拨金额（银行端计算结果）*/
                  'pfs_transfer_amt'    => $val->transferredAmt,
              ]);
        }
    }


    /**
     * 需要执行查询的划拨划拨申请编号
     *
     * @param $pullTime
     *
     * @return array
     */
    public static function listTransferId($pullTime)
    {
        return DB::select('SELECT `id`,`pfs_dw_id`,`apply_date`,`transfer_id` FROM `class_amt_transfer` WHERE `pull_time`>:pull_time AND `result`=:result AND `pfs_transfer_class_num`=:pfs_transfer_class_num AND `is_del`=0 ORDER BY `ctime` DESC', [
            'pull_time'              => $pullTime,
            'result'                 => ClassAmtTransferEnum::RESULT_SUCCESS,
            'pfs_transfer_class_num' => ''
        ]);
    }


    /**
     * 需要处理的业务线-监管账户
     *
     * @return array
     */
    public static function listPsfDw()
    {
        $list = DB::select('SELECT * FROM `pfs_dw_ini` WHERE `is_del`=0');
        $data = [];
        foreach ($list as $val) {
            $data[$val->id] = $val;
        }
        return $data;
    }

}





/**
 * PfsHeadQueryTransferBillModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/7 22:00:00   操作:创建
 **/
